//
//  MemoCtry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/19.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "MemoCtry.h"

#define userKey @"userKey"

@interface MemoCtry ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *SaveBtn;
@property (weak, nonatomic) IBOutlet UITextView *MemoTextView;

@property (nonatomic, strong) NSUserDefaults *defaults;

@end

@implementation MemoCtry

-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    _defaults=[NSUserDefaults standardUserDefaults];
    self.MemoTextView.text=[_defaults objectForKey:userKey];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [self setTextview];
    self.title=@"我的备忘录";
    self.SaveBtn.layer.cornerRadius=10.0;
    //rightNavBtn
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [rightBtn setFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setTitle:@"清除" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    // Do any additional setup after loading the view from its nib.
}
-(void)setTextview{
    // 设置代理
    self.MemoTextView.delegate = self;

    // 设置文本字体
    self.MemoTextView.font = [UIFont fontWithName:@"Arial" size:15];
    self.MemoTextView.textColor=[UIColor blackColor];
    // 设置文本对齐方式
    self.MemoTextView.textAlignment = NSTextAlignmentLeft;
    //自动纠错
self.MemoTextView.autocorrectionType=UITextAutocorrectionTypeNo;
//外框
self.MemoTextView.layer.borderColor=dominantColor.CGColor;
self.MemoTextView.layer.borderWidth=1;
self.MemoTextView.layer.cornerRadius=5;
//不自动大小写
self.MemoTextView.autocapitalizationType=UITextAutocorrectionTypeNo;
// 设置return键样式
    self.MemoTextView.returnKeyType = UIReturnKeyDefault;
// 设置是否可以拖动
    self.MemoTextView.scrollEnabled = YES;
    
}
/**
 将要开始编辑
 @param textView textView
 @return BOOL
 */
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
    
}
/**
 开始编辑
 @param textView textView
 */
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([self.MemoTextView.text isEqualToString:@"请输入您想要记住的东西！"]) {
        
        self.MemoTextView.text = @"";
        self.MemoTextView.textColor=[UIColor blackColor];
    }
    
    
}
/**
 将要结束编辑
 
 @param textView textView
 
 @return BOOL
 */
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    
    return YES;
    
}

/**
 结束编辑
 
 @param textView textView
 */
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.MemoTextView.text.length <1) {
        self.MemoTextView.textColor=[UIColor lightGrayColor];
        self.MemoTextView.text = @"请输入您想要记住的东西！";
    }
   
}

/**
 内容将要发生改变编辑 限制输入文本长度 监听TextView 点击了ReturnKey 按钮
 
 @param textView textView
 @param range    范围
 @param text     text
 
 @return BOOL
 */
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location < 1000)
    {
        return  YES;
        
    } else  if ([self.MemoTextView.text isEqualToString:@"\n"]) {
        
        //这里写按了ReturnKey 按钮后的代码
        return NO;
    }
    
    if (self.MemoTextView.text.length == 1000) {
        
        return NO;
    }
    
   
    return YES;
    
}
//清除按钮
-(void)rightBtnClick:(UIButton *)send{
    if([_MemoTextView.text isEqualToString:@""]){
    
        [MBProgressHUD showError:@"没有内容哦！亲"];
        
    }else{
        
        UIAlertController *alertCtry=[UIAlertController alertControllerWithTitle:@"清空备忘录" message:@"是否清空备忘录所有数据" preferredStyle:UIAlertControllerStyleAlert];
        //弹框的具体内容及响应事件
        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *addAction=[UIAlertAction actionWithTitle:@"清空" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [send setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            send.titleLabel.font=[UIFont systemFontOfSize:17];
            
            [_defaults removeObjectForKey:userKey];
            [_defaults synchronize];
            _MemoTextView.text=nil;
            
            
        }];
        //添加弹框事件到弹框ctry中
        [alertCtry addAction:cancelAction];
        [alertCtry addAction:addAction];
        //把alertCtry 弹框视图推出来
        [self presentViewController:alertCtry animated:YES completion:nil];
    }
    
}
   

- (IBAction)SavebtnAction:(id)sender {
    //保存输入的信息
    NSString *textData=self.MemoTextView.text;
    //创建NSUserDefaults单例
    _defaults=[NSUserDefaults standardUserDefaults];
    //数据写入
    [_defaults setObject:textData forKey:userKey];
    //将数据立即同步到磁盘：
    [_defaults synchronize];
 //   NSLog(@"--_defaults--%@", _defaults);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
        [MBProgressHUD showSuccess:@"保存成功"];
    });
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
